package com.fanyouzhao.joker.service;

import net.sf.json.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * author: fanyouzhao
 * date: 22:35 2021/9/5
 * description: 用token签到，返回签到结果
 */
public class SignIn {
    public static Map<String, String> signIn(String token) {
        Map<String, String> map=new HashMap<>();
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://vip.jokerps.com/wp-json/b2/v1/userMission")
                    .addHeader("Authorization","Bearer "+token)
                    .post(new FormBody.Builder().build())
                    .build();
            Response response = client.newCall(request).execute();
            String responseData = null;
            if (response.body() != null) {
                responseData = response.body().string();
            }
            System.out.println("------------------------签到响应------------------------\n"
                    + responseData);

            if("\"2\"".equals(responseData)){
                map.put("code","1");
                map.put("res",responseData);
                return map;
            }

            JSONObject jsonObject = JSONObject.fromObject(responseData);
            JSONObject mission = jsonObject.getJSONObject("mission");
            // 签到时间
            String date = mission.getString("date");
            map.put("date",date);
            // 本次获得积分
            String credit = mission.getString("credit");
            map.put("credit",credit);
            // 连续签到天数
            String always = mission.getString("always");
            map.put("always",always);
            // 总积分
            String myCredit = mission.getString("my_credit");
            map.put("myCredit",myCredit);
            map.put("code","0");
            return map;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
